package com.live.webvideo.common.easemob;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONObject;


/**
 * 创建环信聊天室
 *
 * @author wangdong
 * @date 2018/11/5 13:51
 */
public class CreateChatRoom {

    private static final Logger logger = LoggerFactory.getLogger(CreateChatRoom.class);
    /**
     *
     * @param roomsName
     * @param desc
     * @param owner
     * @param userIds
     * @return
     *
     * 注：服务器需求格式为
     *          {"name":"testchatroom","description":"server create chatroom","maxusers":300,"owner":"jma1","members":["jma2","jma3"]}
     */
    public static String createChatRooms(String roomsName, String desc, String owner, List<String> userIds) {


        try {
            JSONObject params = new JSONObject();
            params.put("name", roomsName);
            params.put("description", desc);
            // 聊天室成员最大数（包括群主），值为数值类型，默认值200，此属性为可选的
            params.put("maxusers", 5000);
            params.put("owner", owner);

            if (userIds.size() > 0) {
                params.put("members", userIds);
            }

            JSONObject result = HttpTool.doPostsWithToken(HuanxinConstant.CHATROOM_URL, params.toString(), GetAdminToken.getToken());

            if(result.has("error_description")) {
                logger.error("错误信息：" + result.getString("error_description"));
            }

            JSONObject object = result.getJSONObject("data");

            //返回创建好的一个聊天室ID
            if (!(object.get("id") == null)) {
                return object.getString("id");
            }
        } catch (Exception e) {
            return null;
        }

        return null;
    }

    public static void main(String[] args) {
        List<String> userIds = new ArrayList<String>();
        String chatRoom = createChatRooms("600000的聊天室", "大家快来聊天", "8f51baf3a43e2d628d2d0a0465039c3b", userIds);
        System.err.println(chatRoom);
    }
}
